using System;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using PatternsLib.Observer;
using System.Collections;
using System.Collections.Generic;
using FunMazeEngine;

namespace RemotableObjects
{
    
	public class MazeServerObject : MarshalByRefObject, ISubject
	{       
        private List<IObserver> clients = new List<IObserver>();
        private GameController control = new GameController();

        public void SetValue(string clientData)
        {
            Notify(clientData, 0);
        }       

        public MazeServerObject()
		{
            
		}

        public bool CreateNewGame(GamePlayer player, int gridsize, int treasure)
        {
            return control.CreateGame(player, gridsize, treasure); 
        }

        public void GameStarted()
        {
            foreach (GamePlayer player in control.game.gamePlayerList)
            {
             //   player.
            }
        }

        public bool PlayerJoinServer(GamePlayer player){
            if (control.game.NameChecker(player.Name)) { return false; }
            else
            {
                control.game.AddPlayer(player);
                return true;
            }
        }

        public bool PlayerJoinGame(GamePlayer player)
        {
            return control.JoinGame(player);
        }

        public bool GameExists()
        {
            return control.GameExists();
        }

        public bool PlayerMoveEast(GamePlayer player)
        {
            return control.PlayerMoveEast(player);
        }

        public bool PlayerMoveWest(GamePlayer player)
        {
            return control.PlayerMoveWest(player);
        }

        public bool PlayerMoveNorth(GamePlayer player)
        {
            return control.PlayerMoveNorth(player);
        }

        public bool PlayerMoveSouth(GamePlayer player)
        {
            return control.PlayerMoveSouth(player);
        }

        public void GetPlayerLocation(int cell, out int row, out int column)
        {
            control.game.GameMap.GetRowColumnLocation(cell, out row, out column);
        }

        public void QuitGame(GamePlayer player)
        {
            control.game.RemovePlayer(player);
        }

        #region ISubject Members

        public void Attach(IObserver client)
        {
            Console.WriteLine("Plyaer added");
            clients.Add(client);     
        }

        public void Detach(IObserver client)
        {
            clients.Remove(client);
        }

        public bool Notify(string clientData, short objState)
        {
            for (int i = 0; i < clients.Count; i++)
                ((IObserver)clients[i]).Update(this, clientData, objState);

            return true;
        }

        #endregion
    }

    public class MazeClientObject : MarshalByRefObject, IObserver
    {
        private List<Player> playerList = new List<Player>();

        public MazeClientObject()
        {
            
        }       
        

        #region IObserver Members

        bool IObserver.Update(ISubject sender, Object gameInfo, short objState)
        {
            //playerList.Add(player);
            return true;
        }

        #endregion
    }


}